/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sd.rmi.auction.client;

import java.io.Serializable;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.logging.Level;
import java.util.logging.Logger;
import sd.rmi.auction.interfaces.IAuction;
import sd.rmi.auction.interfaces.INotifiable;

/**
 *
 * @author blackdevil
 */
public class ClientProxy implements IAuction,INotifiable,Serializable {

    final static String serverUrlLocale = "127.0.0.1";
    final static String serverUrlExt = "";
    final static String serverUrlLan = "";
    private IAuction stub = null;
    private String serviceName = null;

    public ClientProxy(String serviceName) {

        this.serviceName = serviceName;
        try {
            System.out.println("Locate registry");
            Registry registry = LocateRegistry.getRegistry(serverUrlLocale);
            System.out.println("Service lookup");
            this.stub = (IAuction) registry.lookup(serviceName);
            stub.registerForNotification(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setBid(int bid)  {
        
        try {
            stub.setBid(bid);
        } catch (RemoteException ex) {
            Logger.getLogger(ClientProxy.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
    }

    @Override
    public int getCurrentBid() {
        try {
            return stub.getCurrentBid();
        } catch (RemoteException ex) {
            Logger.getLogger(ClientProxy.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
            return 0;
        }
    }

    @Override
    public String getProduct() {
        try {
            return stub.getProduct();
        } catch (RemoteException ex) {
            Logger.getLogger(ClientProxy.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
            return "";
        }
    }

    @Override
    public void registerForNotification(INotifiable n) {
        try {
            stub.registerForNotification(this);
        } catch (Exception ex) {
            Logger.getLogger(ClientProxy.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
    }

    public void notify(int number) throws RemoteException {
        System.out.println("Notify dal server!!!");
        this.getCurrentBid();
    }
}
